package main

import (
	"fmt"
	"natsmicro/conf"
	"os"
	"strconv"
	"sync"

	"github.com/gogf/gf/frame/g"

	"github.com/nats-io/nats.go"
)

var donewg sync.WaitGroup

func main() {
	ext := "100"
	NN := 100000
	if len(os.Args) == 3 {
		ext = os.Args[1]
		NN, _ = strconv.Atoi(os.Args[2])
	}
	nc, _ := nats.Connect(conf.NatsUrl)
	js, err := nc.JetStream()

	if err != nil {
		g.Log().Fatal(err)
	}
	donewg.Add(NN)

	_, err = js.AddStream(&nats.StreamConfig{
		Name:     conf.ConfigName,
		Subjects: []string{conf.StreamSubjectName},
	})
	fmt.Println("err", err)

	for ii := 0; ii < NN; ii++ {
		var jj = ii
		go func() {
			js.Publish(conf.SubjectName+ext, []byte("Hello World "+strconv.Itoa(jj)))
			donewg.Done()
		}()
	}
	donewg.Wait()
	nc.Close()
}
